package com.ruoyi.hospital.medicine.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.hospital.medication.domain.TMedication;
import com.ruoyi.hospital.medication.service.ITMedicationService;
import com.ruoyi.hospital.medicine.domain.TPatientMedicine;
import com.ruoyi.hospital.medicine.mapper.TPatientMedicineMapper;
import com.ruoyi.hospital.medicine.service.ITPatientMedicineService;
import com.ruoyi.hospital.registered.domain.TRegistered;
import com.ruoyi.hospital.registered.mapper.TRegisteredMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 患者药品关联Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Service
public class TPatientMedicineServiceImpl implements ITPatientMedicineService {
    @Autowired
    private TPatientMedicineMapper tPatientMedicineMapper;

    @Autowired
    private ITMedicationService insertTPatientMedicine;

    @Autowired
    private TRegisteredMapper tRegisteredMapper;

    /**
     * 查询患者药品关联
     *
     * @param id 患者药品关联主键
     * @return 患者药品关联
     */
    @Override
    public TPatientMedicine selectTPatientMedicineById(Long id) {
        return tPatientMedicineMapper.selectTPatientMedicineById(id);
    }

    /**
     * 查询患者药品关联列表
     *
     * @param tPatientMedicine 患者药品关联
     * @return 患者药品关联
     */
    @Override
    public List<TPatientMedicine> selectTPatientMedicineList(TPatientMedicine tPatientMedicine) {
        return tPatientMedicineMapper.selectTPatientMedicineList(tPatientMedicine);
    }

    @Override
    public List<TPatientMedicine> queryByregistered(String registeredId) {
        TPatientMedicine tPatientMedicine = new TPatientMedicine();
        tPatientMedicine.setRegisteredId(registeredId);
        return tPatientMedicineMapper.selectTPatientMedicineList(tPatientMedicine);
    }

    /**
     * 新增患者药品关联
     *
     * @param tPatientMedicine 患者药品关联
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTPatientMedicine(TPatientMedicine tPatientMedicine) {
        TRegistered tRegistered = tRegisteredMapper.selectTRegisteredById(Long.parseLong(tPatientMedicine.getRegisteredId()));
        if (null == tRegistered) {
            throw new ServiceException("门诊不存在");
        }
        tPatientMedicine.setCreatedTime(new Date());
        tPatientMedicine.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        tPatientMedicine.setPatientId(String.valueOf(tRegistered.getPatientId()));
        tPatientMedicineMapper.insertTPatientMedicine(tPatientMedicine);
        return deductionInventory(tPatientMedicine);
    }

    private int deductionInventory(TPatientMedicine tPatientMedicine) {
        TMedication tMedication = new TMedication();
        tMedication.setId(Long.parseLong(tPatientMedicine.getMedicationId()));
        tMedication.setInventory(tPatientMedicine.getQuantity());
        return insertTPatientMedicine.deductionInventory(tMedication);
    }

    /**
     * 修改患者药品关联
     *
     * @param tPatientMedicine 患者药品关联
     * @return 结果
     */
    @Override
    public int updateTPatientMedicine(TPatientMedicine tPatientMedicine) {
        return tPatientMedicineMapper.updateTPatientMedicine(tPatientMedicine);
    }

    /**
     * 批量删除患者药品关联
     *
     * @param ids 需要删除的患者药品关联主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTPatientMedicineByIds(String ids) {
        String[] array = Convert.toStrArray(ids);
        TPatientMedicine tPatientMedicine = tPatientMedicineMapper.selectTPatientMedicineById(Long.parseLong(array[0]));
        if (null == tPatientMedicine) {
            throw new ServiceException("数据不存在");
        }
        increaseInventory(tPatientMedicine);
        return tPatientMedicineMapper.deleteTPatientMedicineByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除患者药品关联信息
     *
     * @param id 患者药品关联主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTPatientMedicineById(Long id) {
        TPatientMedicine tPatientMedicine = tPatientMedicineMapper.selectTPatientMedicineById(id);
        if (null == tPatientMedicine) {
            throw new ServiceException("数据不存在");
        }
        increaseInventory(tPatientMedicine);
        return tPatientMedicineMapper.deleteTPatientMedicineById(id);
    }

    private int increaseInventory(TPatientMedicine tPatientMedicine) {
        TMedication tMedication = new TMedication();
        tMedication.setId(Long.parseLong(tPatientMedicine.getMedicationId()));
        tMedication.setInventory(tPatientMedicine.getQuantity());
        return insertTPatientMedicine.increaseInventory(tMedication);
    }
}
